package com.bidevalution.controller;

import com.bidevalution.properties.SecurityProperties;
import com.bidevalution.service.TbFileService;
import com.bidevalution.service.impl.TbFileServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@EnableConfigurationProperties(SecurityProperties.class)
@RequestMapping("/api")
public class FileDownController {
    private static final Logger log = LoggerFactory.getLogger(TbFileServiceImpl.class);
    @Autowired(required = false)
    TbFileService tbFileService;
    @RequestMapping(value = {"fileDownResult"})
    @ResponseBody
    public void fileDownResult(HttpServletRequest request, HttpServletResponse response)  {
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Cache-Control", "no-store");
        String isSuccess = request.getParameter("isSuccess");
        String guid = request.getParameter("guid");
        String id = request.getParameter("id");
        String sectionCode = request.getParameter("sectionCode");
        String error = request.getParameter("error");
        try {
            if ("false".equals(isSuccess)) {
                tbFileService.askRePushFile(sectionCode, guid, error);
                Thread.sleep(2000);
            } else {
                tbFileService.updateFileDownResult(id);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

    }
}
